package com.sean.main;

import com.sean.main.domain.VideoDO;
import com.sean.main.service.impl.VideoServiceImpl;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;


public class Main {
    static VideoServiceImpl videoService;
    static ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-db.xml");


    public static void main(String[] args) {
        videoService = ctx.getBean(VideoServiceImpl.class);
        while(true) {
            List<VideoDO> videos = videoService.listUnQueue();
            for (VideoDO v : videos) {
                WebDriver driver = getDriver();
                String url = v.getUrl().contains("?")?v.getUrl()+"&":v.getUrl()+"?";
                url+="zcahvideoid=" + v.getIdvideo();
                try {
                    System.out.println(url.substring(0, url.lastIndexOf("?")) + URLEncoder.encode(url.substring(url.lastIndexOf("?")), "utf8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                try {
                    driver.get("https://baike.baidu.com/item/%E7%8A%B0%E7%8B%B3/522688%3fsecondId=318902&zcahvideoid=2001");
//                    driver.get(url.substring(0, url.lastIndexOf("?")) + URLEncoder.encode(url.substring(url.lastIndexOf("?")), "utf8"));
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    driver.close();
                }
            }
        }
    }

    private static WebDriver getDriver() {
        System.setProperty("webdriver.gecko.driver", "/opt/geckodriver");
        System.setProperty("webdriver.firefox.bin", "/opt/firefox/firefox/firefox-bin");



        File file = new File("/root/.mozilla/firefox/ufzod8z3.default");
        FirefoxProfile fileprofile = new FirefoxProfile(file);
        fileprofile.setPreference("dom.ipc.plugins.enabled.libflashplayer.so", "true");
        fileprofile.setPreference("plugin.state.flash", 2);
        FirefoxOptions options = new FirefoxOptions().setProfile(fileprofile);
        WebDriver driver = new FirefoxDriver(options);



       /* ProfilesIni allProfiles = new ProfilesIni();
        FirefoxProfile profile = allProfiles.getProfile("kexie");

        System.out.println(profile.getStringPreference("dom.ipc.plugins.enabled.libflashplayer.so","null"));
        profile.setPreference("dom.ipc.plugins.enabled.libflashplayer.so", "true");
        profile.setPreference("plugin.state.flash", 2);
        System.out.println(profile.getStringPreference("plugin.state.flash", "null"));
        FirefoxOptions options = new FirefoxOptions().setProfile(profile);
        WebDriver driver = new FirefoxDriver();*/


       /* String plugin = "/home/sean/Desktop/controller.xpi";
        try {
            profile.addExtension(new File(plugin));
            profile.setPreference("permissions.default.stylesheet", 2);
            profile.setPreference("permissions.default.image", 2);

        } catch (Exception e) {
            e.printStackTrace();
        }*/


        return driver;
    }
}
